// pages/person/maincoupon/index.js
const app = getApp()
Page({
  data: {
    tablist: [{
      value: "可使用"
    }, {
      value: "已使用"
    }, {
      value: "已过期"
    }],
    tabIndex: 0, //tab栏默认选中第一个
    cardlist: [],
    store_id: 0,
    from: '',
    isCardList: true
  },
  onLoad(options) {
    wx.setStorageSync('coupon', '')
    const that = this
    console.log(options)
    if (options.from == "storeOrder") {

      that.setData({
        from: options.from,
        order_on: options.order_on
      })
//可使用

      that.getUsecoupon()
    } else {
      //我的
      that.getMinecoupon()
    }

  },

  // 去使用
  gotoIndex(e) {
    const that = this
    // console.log(e.currentTarget.dataset)
    if (that.data.from == "") {
      wx.reLaunch({
        url: '/pages/index/index',
      })
    } else {
      wx.setStorageSync('coupon', e.currentTarget.dataset.data)
      wx.navigateBack({
        dataset: 1
      })
    }

  },
  // 可使用的优惠券
  getUsecoupon() {
    const that = this
    app.http.post(app.api.CouponOrder, {
      order_on: that.data.order_on
    }, (res) => {
      console.log(res)
      that.setData({
        cardlist: res.data.lists,
        isCardList: false
      })
    })
  },
  //我的优惠券
  getMinecoupon() {
    const that = this
    app.http.post(app.api.userCoupon, {
      status: that.data.tabIndex
    }, (res) => {
      if (res.data.lists == "") {
        that.setData({
          cardlist: res.data.lists,
          isCardList: true
        })
      } else {
        that.setData({
          cardlist: res.data.lists,
          isCardList: false
        })
      }

    })
  },
  // 切换tab栏
  chooseTab(e) {
    const that = this
    that.setData({
      tabIndex: e.currentTarget.dataset.id,
    });
    console.log(that.data.tabIndex)
    if (that.data.tabIndex == 0) {
      if (that.data.from == "") {
        that.getMinecoupon()
      } else {
        that.getUsecoupon()
      }
    } else {
      that.getMinecoupon()
    }

  },
});